import Vue from 'vue';
import Router from 'vue-router';

import Login from '../components/page/Login'
import Home from '../components/common/Home'
import Dashboard from '../components/page/Dashboard'
import OrderTable from '../components/page/OrderTable.vue'
import ProductTable from '../components/page/productTable';
import UserTable from '../components/page/userTable';
import MessageTabs from '../components/page/MessageTabs';
import Page404 from '../components/page/404'
import Page403 from '../components/page/403'

Vue.use(Router);


const routes = [
    {
        path: '/',
        component: Login
    },
    {
        path: '/',
        component: Home,
        meta: { title: '首页' },
        children: [
            {
                path: '/dashboard',
                component: Dashboard,
                meta: { title: '系统首页' }
            },
            {
              path: '/userTable',
              component: UserTable,
              meta: { title: '用户表格' }
            },
            {
              path: '/productTable',
              component: ProductTable,
              meta: { title: '商品表格' }
            },
            {
                path: '/orderTable',
                component: OrderTable,
                meta: { title: '订单管理' }
            },
            {
                path: '/userTable',
                component: UserTable,
                meta: { title: '用户表格' }
            },
            {
                path: '/messageTabs',
                component: MessageTabs,
                meta: { title: '消息中心' }
            },
            {
                path: '/404',
                component: Page404,
                meta: { title: '404' }
            },
            {
                path: '/403',
                component: Page403,
                meta: { title: '403' }
            }
        ]
    },
    {
        path: '/login',
        component: Login,
        meta: { title: '登录' }
    },
    {
        path: '*',
        redirect: '/404'
    }
 ]

 const router = new Router({
   routes
 })

 // 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
 router.beforeEach((to, from, next) => {
   // 访问登录页，放行
   if (to.path === '/login') return next()
   // 获取token
   const tokenStr = window.sessionStorage.getItem('ms_username')
   // 没有token, 强制跳转到登录页
   if (!tokenStr) return next('/login')
   next()
 })

 export default router

